﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CPPEI.Coolzon.DesignServer.Domain.Services
{
    public class WorkspaceService
    {
        /// <summary>
        /// 创建工作空间
        /// </summary>
        /// <param name="localPath"></param>
        /// <param name="userIdentity"></param>
        /// <param name="computer"></param>
        /// <returns></returns>
        public SingleResult<Entities.Workspace> Create(string localPath, string userIdentity, string computer)
        {
            using(DSDBContext dbContext = new DSDBContext())
            {
                Entities.Workspace workspace = new Entities.Workspace();
                workspace.Id = Guid.NewGuid();
                workspace.CreatedTime = DateTime.Now;
                workspace.Creator = userIdentity;
                workspace.LocalPath = localPath;
                workspace.Computer = computer;
                workspace.IsDeleted = false;
                workspace.IsEnable = true;

                Repositories.EntityRepository<Entities.Workspace> rep = new Repositories.EntityRepository<Entities.Workspace>(dbContext);
                rep.Create(workspace);

                if (dbContext.SaveChanges() > 0)
                {

                    //CPPEI.Coolzon.Common.Logger.InfoToTag("WorkspaceService", string.Format("用户{0}创建工作空间成功,(localPath:{1},computer:{2})", userIdentity, localPath, computer));
                    return new SingleResult<Entities.Workspace>(true, 0, string.Empty, workspace);
                }
               // CPPEI.Coolzon.Common.Logger.InfoToTag("WorkspaceService", string.Format("用户{0}创建工作空间失败,(localPath:{1},computer:{2})", userIdentity, localPath, computer));
                return new SingleResult<Entities.Workspace>(false, 1, string.Empty, null);
            }
        }

        /// <summary>
        /// 获取用户可用的工作空间
        /// </summary>
        /// <param name="userIdentity"></param>
        /// <param name="computer"></param>
        /// <returns></returns>
        public ListResult<Entities.Workspace> Find(string userIdentity, string computer)
        {
            using (DSDBContext dbContext = new DSDBContext())
            {
                Repositories.EntityRepository<Entities.Workspace> rep = new Repositories.EntityRepository<Entities.Workspace>(dbContext);
                List<Entities.Workspace> list = rep.GetList(p => p.Creator == userIdentity 
                    && p.Computer == computer
                    && p.IsDeleted == false && p.IsEnable);
               // CPPEI.Coolzon.Common.Logger.InfoToTag("WorkspaceService", string.Format("获取用户:{0},computer:{}可用的工作空间成功.", userIdentity, computer));
                return new ListResult<Entities.Workspace>(true, 0, string.Empty, list);
            }
        }
    }
}
